package com.smartstate.iot.mqtt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.entity.vo.GatewayInfoVo;
import com.smartstate.iot.mapper.TopicLogMapper;
import com.smartstate.iot.mapper.TopicMapper;
import com.smartstate.iot.mqtt.service.TopicService;
import com.smartstate.iot.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class GatewayTopicImpl extends ServiceImpl<TopicMapper, TopicConfig> implements TopicService {

    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private TopicLogMapper topicLogMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有盒子信息
     *
     * @return
     */
    @Override
    public List<GatewayInfoVo> getAll() {
        List<TopicConfig> topicConfigs = topicMapper.selectList(new LambdaQueryWrapper<>());
        ArrayList<GatewayInfoVo> gatewayInfos = new ArrayList<>();
        for (TopicConfig topicConfig : topicConfigs) {
            String type = topicLogMapper.getGatewayInfo(topicConfig.getDeviceId());
            GatewayInfoVo gatewayInfo = new GatewayInfoVo();
            gatewayInfo.setDeviceStatus(type);
            gatewayInfo.setDeviceIp(topicConfig.getIp());
            gatewayInfo.setDeviceName(topicConfig.getDeviceName());
            gatewayInfo.setDevicePort(topicConfig.getPort());
            gatewayInfos.add(gatewayInfo);
        }
//        List<GatewayInfoVo> boxInfo = topicMapper.getBoxInfo();
        return gatewayInfos;
    }

    /**
     * 查询盒子cpu使用率
     *
     * @param deviceName
     * @return
     */
    @Override
    public int getCpuInfo(String deviceName) {
        LambdaQueryWrapper<TopicLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TopicLog::getLogName, deviceName);
        queryWrapper.orderByDesc(TopicLog::getDate);
        queryWrapper.last("limit 1");
        TopicLog topicLog = topicLogMapper.selectOne(queryWrapper);
        if ((topicLog != null && "离线".equals(topicLog.getType()))) {
            return 0;
        }
        if (topicLog!=null&&topicLog.getDeviceId() != null) {
            Integer cpuInfo = topicMapper.getCpuInfo(topicLog.getDeviceId());
            return cpuInfo;
        } else {
            return 0;
        }
    }

    /**
     * 查询内存使用率
     *
     * @param deviceName
     * @return
     */
    @Override
    public int getMemoryInfo(String deviceName) {
        LambdaQueryWrapper<TopicLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TopicLog::getLogName, deviceName);
        queryWrapper.orderByDesc(TopicLog::getDate);
        queryWrapper.last("limit 1");
        TopicLog topicLog = topicLogMapper.selectOne(queryWrapper);
        if ((topicLog != null && "离线".equals(topicLog.getType()))) {
            return 0;
        }
        if (topicLog != null && topicLog.getDeviceId() != null) {
            Integer memory = topicMapper.getMemory(topicLog.getDeviceId());
            return memory;
        } else {
            return 0;
        }
    }

    /**
     * 查看盒子与设备所属关系
     *
     * @param deviceId
     * @return
     */
    @Override
    public ResultVO getDetails(String deviceId) {
        TreeSet<String> objects = new TreeSet<>();
        if (deviceId == null || deviceId == "") {
            return ResultVO.fail("参数错误!");
        }
        String newDeviceId = topicMapper.getDeviceIdByDeviceName(deviceId);
        Set keys = redisTemplate.keys(newDeviceId + ":*");
//        Set keys = redisTemplate.keys("WG001" + ":*");
//        log.info(keys.toString());
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            String next = (String) iterator.next();
            int lastIndexOf = next.lastIndexOf(":");
            int index = next.indexOf(":");
            String substring = next.substring(index + 1,lastIndexOf);
            objects.add(substring);
        }
        if (objects.size()==0){
            return ResultVO.ok("暂无设备!");
        }
        return ResultVO.ok(objects);
    }
}
